#ifndef HOSTLIST_H
#define HOSTLIST_H

#include <QAbstractListModel>
#include <QList>
#include <QString>
#include <QHash>
#include <QByteArray>
#include <QDateTime>
#include "SqliteHelper.h"
namespace Moolink {


    class Host
    {
    public:

        Host(QString name_, QString hostip_,int hostprot_=8234,  QString protocol_="HJ212_2017", QString color_="gray"){
            HostName = name_;
            HostIp=hostip_;
            HostPort=hostprot_;
            protocol=protocol_;
            colorCode = color_;
        }
        Host(QVariant hostVar){

            id=hostVar.value<QVariantMap>()["serverId"].toString();
            HostName=hostVar.value<QVariantMap>()["serverName"].toString();
            HostIp=hostVar.value<QVariantMap>()["serverIp"].toString();
            HostPort=hostVar.value<QVariantMap>()["serverPort"].toInt();
            stationMN=hostVar.value<QVariantMap>()["stationMN"].toString();
            stationPW=hostVar.value<QVariantMap>()["stationPW"].toString();

            protocol=hostVar.value<QVariantMap>()["protocolName"].toString();
            protocolFlag=hostVar.value<QVariantMap>()["protocolFlag"].toInt();
        }

    public:
        QString id;
        QString HostName;
        QString HostIp;
        int HostPort;
        QString stationMN;
        QString stationPW;
        QString protocol;
        qint8 protocolFlag;
        QString colorCode="gray";
        QString dataString;
    };

    class HostList : public QAbstractListModel
    {

        Q_OBJECT

    public:
    //    QString name;
    //    QString portname;
    //    QString protocol;
    //    int comaddr;
    //    QString colorCode;
        enum LineInfo_Roles{
            RoleName =Qt::UserRole + 1,
            RoleHostIp,
            RoleHostPort,
            RoleProtocol,
            RolecolorCode,
            RoledataString
        };


    public:
        explicit HostList(QObject *parent = 0);

        int rowCount(const QModelIndex &parent) const;
        int rowCountRead();
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const;
        //Q_INVOKABLE void addHost(QString name_, QString hostip_, QString hostprot_, QString protocol_);
        void addHost(QVariant hostVar);
        QList<Host> getdata(){return m_display_list;}
        int getdataIndexById(QString serverId);
        int getdataSize(){return m_display_list.size();}
        Host getdataByIndex(int index){return m_display_list[index];}
        Host* getpdataByIndex(int index){return &m_display_list[index];}
        void changeDataString(int index,QString str,int error=0);

        QString getMNByIndex(int index){return m_display_list[index].stationMN;}
        QString getPWByIndex(int index){return m_display_list[index].stationPW;}
        qint8 getFlagByIndex(int index){return m_display_list[index].protocolFlag;}

        Q_INVOKABLE void changeName(int index);
        Q_INVOKABLE void clickItem(int index);

    signals:
        void dataChanged();
    public slots:

    private:
        QList<Host> m_display_list;
        QHash<int, QByteArray> m_roleNames;
    };
}
#endif // HOSTLIST_H
